/*
 * chat_srv.c
 *
 *  Created on: Apr 3, 2012
 *      Author: root
 */

/* include fig01 */
#include	"unp.h"
//ssize_t readcrlf(int fd, void *vptr, size_t maxlen);
#define USER "230 User logged in\r\n\r\n"
#define TO   "200 OK\r\n\r\n"
#define QUIT "221 service closing\r\n\r\n"

int
main(int argc, char **argv)
{
	int					i, maxi, maxfd, listenfd, connfd, sockfd;
	int 				count=0, a=0;
	int size=10;
	int					nready, client[size];
	ssize_t				n;
	fd_set				rset, allset;
	char				buf[MAXLINE],buf1[MAXLINE];
	char 				userlist[10];
	char 				*command,*ID;


	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;

	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	maxfd = listenfd;			/* initialize */
	maxi = -1;					/* index into client[] array */
	for (i = 0; i < size; i++)
		{client[i] = -1;			/* -1 indicates available entry */

		printf("client[%d] : %d\n",i,client[i]);
		}
	FD_ZERO(&allset);
	FD_SET(listenfd, &allset);
/* end fig01 */

/* include fig02 */
	for ( ; ; ) {

		rset = allset;		/* structure assignment */
		nready = Select(maxfd+1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(listenfd, &rset)) {	/* new client connection */
			clilen = sizeof(cliaddr);
			connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
#ifdef	NOTDEF
			printf("new client: %s, port %d\n",
					Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL),
					ntohs(cliaddr.sin_port));
#endif

			for (i = 0; i < size; i++)
				if (client[i] < 0) {
					client[i] = connfd;	/* save descriptor */
					printf("save client[%d] : %d\n",i,client[i]);
					count++;
					break;
				}
			if (i == size)
				err_quit("too many clients");

			FD_SET(connfd, &allset);	/* add new descriptor to set */
			if (connfd > maxfd)
				{maxfd = connfd;			/* for select */
				printf("maxfd : %d\n",maxfd);
				}
			if (i > maxi)
				{maxi = i;				/* max index in client[] array */
			printf("maxi : %d\n",maxi);
				}
			if (--nready <= 0)
				continue;				/* no more readable descriptors */
		}

		for (i = 0; i <= maxi; i++) {	/* check all clients for data */
			if ( (sockfd = client[i]) < 0)
				{printf("continue\n");
				continue;}
			if (FD_ISSET(sockfd, &rset)) {
				if ( (n = read(sockfd, buf,MAXLINE)) == 0) {
						/*4connection closed by client */
					Close(sockfd);
					FD_CLR(sockfd, &allset);
					client[i] = -1;
					printf("close client[%d]\n",i);
					count--;
				} else

				sprintf(buf1,"%s",buf);


				printf("--------------------------\n");
			    command=strtok(buf," ,\r");
				if(!strcmp(buf,"USER"))
				{
					   while(command !=NULL)
					{
					printf("111111%s\n",command);
					command = strtok(NULL," ,\r\n\r\n");
					}
					printf("%s\n",command);
		        Writen(sockfd,USER,strlen(USER));
				}

				//command=strtok(buf," ,\r");

				if(!strcmp(buf,"WHO\r\n\r\n"))
				{

				}


				if(!strcmp(buf,"QUIT"))
				{

					printf("-----------------close----");
					//memset(buf,0,strlen(buf));
					Writen(sockfd,QUIT,strlen(QUIT));
					printf("%d\n",strlen(buf));
					exit(0);

				}


					printf("aaaaaaaaaaaaaaaaaa\n");
					Fputs(buf,stdout);
					printf("aaaaaaaaaaaaaaaaaaaaa\n");






				    printf("-------%d------\n",client[0]);
				    printf("-------%d------\n",sockfd);
					for(a=0; a<count; a++)
					{
						if(client[a] != sockfd)
						Writen(client[a],buf,strlen(buf));
						//Write(fileno(stdout),buf,strlen(buf));
					}

				//Writen(sockfd,buf,n);
				//Fputs(buf,stdout);
					printf("sockfd : %d\n",sockfd);
					printf("client[%d] : %d\n",0,client[0]);
					printf("client[%d] : %d\n",1,client[1]);
					printf("client[%d] : %d\n",2,client[2]);
					printf("count count : %d\n\n",count);
					memset(buf,0,strlen(buf));

				if (--nready <= 0)
					break;				/* no more readable descriptors */
			}
		}
	}
}
/* end fig02 */

